<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="系统类型:" prop="systemType">
        <el-select v-model="queryParams.systemType" placeholder="请选择系统类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_system_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable size="small">
          <el-option
            v-for="dict in dict.type.pub_notice_plus_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.pub_notice_plus_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <!--
      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择部门" />
      </el-form-item>
      -->

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pub:noticePlus:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pub:noticePlus:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticePlusList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="标题" show-overflow-tooltip align="center" prop="title">
        <template slot-scope="scope">
          <span @click="handleLook(scope.row)" class="link-type" >{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pub_notice_plus_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pub_notice_plus_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="系统类型" align="center" prop="systemType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_system_type" :value="scope.row.systemType"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="摘要" show-overflow-tooltip align="center" prop="summary" /> -->
      <!--<el-table-column label="内容" show-overflow-tooltip align="center" prop="content" >
        <template slot-scope="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>-->
      <el-table-column label="生效时间" align="center" prop="noticeStartTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.noticeStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="noticeEndTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.noticeEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者"  align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status=='0' || scope.row.status=='2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pub:noticePlus:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.status=='0' || scope.row.status=='2'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pub:noticePlus:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.status=='1'"
            size="mini"
            type="text"
            icon="el-icon-close-notification"
            @click="handleUpdateStatus(scope.row,2)"
            v-hasPermi="['pub:noticePlus:remove']"
          >撤销</el-button>
          <el-button
            v-if="scope.row.status=='0' || scope.row.status=='2'"
            size="mini"
            type="text"
            icon="el-icon-bell"
            @click="handleUpdateStatus(scope.row,1)"
            v-hasPermi="['pub:noticePlus:remove']"
          >发布</el-button>
          <el-button
            v-if="scope.row.type=='1'"
            size="mini"
            type="text"
            icon="el-icon-s-custom"
            @click="selectReceiverBatch(scope.row)"
            v-hasPermi="['pub:noticePlus:receiver']"
          >接收人</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统公告通知对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" :close-on-click-modal="false" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="系统类型" prop="systemType">
          <el-select v-model="form.systemType" placeholder="请选择系统类型">
            <el-option
              v-for="dict in dict.type.sys_system_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.pub_notice_plus_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="摘要" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" type="textarea" :rows="4" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="生效时间" prop="noticeStartTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.noticeStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="noticeEndTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.noticeEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog v-dialogDrag :title="titleLook" :visible.sync="openLook" :close-on-click-modal="false" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" disabled/>
        </el-form-item>
        <el-form-item label="系统类型" prop="systemType">
          <el-select v-model="form.systemType" placeholder="请选择系统类型" disabled>
            <el-option
              v-for="dict in dict.type.sys_system_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="form.type">
            <el-radio disabled
              v-for="dict in dict.type.pub_notice_plus_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="摘要" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" type="textarea" :rows="4" placeholder="请输入内容" disabled></el-input>
        </el-form-item>
        <el-form-item label="生效时间" prop="noticeStartTime">
          <el-date-picker clearable size="small" style="width: 200px" disabled
            v-model="form.noticeStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="noticeEndTime">
          <el-date-picker clearable size="small" style="width: 200px" disabled
            v-model="form.noticeEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelLook">关 闭</el-button>
      </div>
    </el-dialog>

    <select-receiver ref='openReceiver' :notice='notice' @ok="handleQuery" />
  </div>
</template>

<script>
import { listNoticePlus, getNoticePlus, delNoticePlus, addNoticePlus, updateNoticePlus, exportNoticePlus } from "@/api/pub/noticePlus";
import Editor from '@/components/Editor';
import selectReceiver from "./selectReceiver";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "NoticePlus",
  dicts: ['pub_notice_plus_type', 'pub_notice_plus_status', 'sys_system_type'],
  components: {
    Editor,
    Treeselect,
    selectReceiver
  },
  data() {
    return {
      notice:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统公告通知表格数据
      noticePlusList: [],
      // 部门树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      openLook: false,
      titleLook: '查看',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        summary: null,
        content: null,
        status: null,
        systemType: null,
        type: null,
        noticeStartTime: null,
        noticeEndTime: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        //status: [
        //  { required: true, message: "状态不能为空", trigger: "blur" }
        //],
        //systemType: [
        //  { required: true, message: "系统类型不能为空", trigger: "change" }
        //],
        // noticeStartTime: [
        //   { required: true, message: "生效时间不能为空", trigger: "blur" }
        // ],
        // noticeEndTime: [
        //   { required: true, message: "截止时间不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    replaceImg(value){
      return value=value.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, "")
    },
    handleUpdateStatus(row,status){
      if (row != null) {
        row.status=status;
        updateNoticePlus(row).then(response => {
          this.msgSuccess("操作成功");
          this.getList();
        });
      } else {
      }
    },
    selectReceiverBatch(row) {
      this.reset();
      this.notice = row;
      setTimeout( ()=> {
        this.$refs.openReceiver.show();
      },500);
    },
    /** 查询系统公告通知列表 */
    getList() {
      this.loading = true;
      listNoticePlus(this.queryParams).then(response => {
        response.rows.forEach(ele=>{
          if(ele.content){
            ele.content=this.replaceImg(ele.content);
          }
        })
        this.noticePlusList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelLook() {
      this.openLook = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        summary: null,
        content: null,
        status: null,
        systemType: null,
        type: '1',
        noticeStartTime: null,
        noticeEndTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统公告通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNoticePlus(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改系统公告通知";
      });
    },
    handleLook(row) {
      this.reset();
      const id = row.id || this.ids
      getNoticePlus(id).then(response => {
        this.form = response.data;
        this.openLook = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNoticePlus(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNoticePlus(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除系统公告通知编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNoticePlus(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有系统公告通知数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportNoticePlus(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },

  }
};
</script>
